from django import http
from django.core.urlresolvers import reverse
from django.utils.functional import wraps
from django.utils.http import urlquote
from django.views.generic.simple import direct_to_template

import django_community.utils

def user_required(func):
    """
    A decorator that redirects the user to the user-required view if they are not logged in
    """
    @wraps(func)
    def dec(request, *args, **kwargs):
        if not django_community.utils.get_logged_user(request):
            url = reverse('community-combined-login') + '?redirect=%s' % (request.path)
            return http.HttpResponseRedirect(url)
        return func(request, *args, **kwargs)
    return dec

def anonymous_required(func):
    """
    A decorator that redirects the user to the anonymous-required view if they are not logged in
    """
    @wraps(func)
    def dec(request, *args, **kwargs):
        if django_community.utils.get_logged_user(request):
            url = reverse('anonymous-required')
            return http.HttpResponseRedirect(url)
        return func(request, *args, **kwargs)
    return dec
